package com.jianyun.jyzs.utils;

import com.prolificinteractive.materialcalendarview.CalendarDay;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    /**
     * 得到当前时间
     *
     * @param dateFormat 时间格式
     * @return 转换后的时间格式
     */
    public static String getStringToday(String dateFormat) {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 将字符串型日期转换成日期
     *
     * @param dateStr    字符串型日期
     * @param dateFormat 日期格式
     * @return
     */
    public static Date stringToDate(String dateStr, String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        try {
            return formatter.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String dateToString(Date date, String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(date);
    }

    /**
     * 两个时间点的间隔时长（分钟）
     *
     * @param before 开始时间
     * @param after  结束时间
     * @return 两个时间点的间隔时长（分钟）
     */
    public static long compareMin(Date before, Date after) {
        if (before == null || after == null) {
            return 0l;
        }
        long dif = 0;
        if (after.getTime() >= before.getTime()) {
            dif = after.getTime() - before.getTime();
        } else if (after.getTime() < before.getTime()) {
            dif = after.getTime() + 86400000 - before.getTime();
        }
        dif = Math.abs(dif);
        return dif / 60000;
    }

    /**
     * 获取指定时间间隔分钟后的时间
     *
     * @param date 指定的时间
     * @param min  间隔分钟数
     * @return 间隔分钟数后的时间
     */
    public static Date addMinutes(Date date, int min) {
        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, min);
        return calendar.getTime();
    }

    /**
     * 根据时间返回指定术语，自娱自乐，可自行调整
     *
     * @param hourday 小时
     * @return
     */
    public static String showTimeView(int hourday) {
        if (hourday >= 22 && hourday <= 24) {
            return "晚上";
        } else if (hourday >= 0 && hourday <= 6) {
            return "凌晨";
        } else if (hourday > 6 && hourday <= 12) {
            return "上午";
        } else if (hourday > 12 && hourday < 22) {
            return "下午";
        }
        return null;
    }

    public static String getScheduleTime(int year, int monthOfYear, int dayOfMonth, int hour, int minute) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(year).append("/")
                .append(getFormatTime(monthOfYear)).append("/")
                .append(getFormatTime(dayOfMonth)).append(" ")
                .append(getFormatTime(hour)).append(":")
                .append(getFormatTime(minute));
        return stringBuilder.toString();
    }

    private static String getFormatTime(int time) {
        StringBuilder stringBuilder = new StringBuilder();
        if (time < 10) {
            stringBuilder.append("0").append(time);
        } else {
            stringBuilder.append(time);
        }
        return stringBuilder.toString();
    }

    public static String getDefaultScheduleTime() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);

        return getScheduleTime(year, monthOfYear + 1, dayOfMonth, hour, minute);//calendar中月份从0开始；
    }

    public static String getScheduleTime(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int year = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        if (time > getTodayMorning() && time < getTodayNight()) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder
                    .append(getFormatTime(hour)).append(":")
                    .append(getFormatTime(minute));
            return stringBuilder.toString();
        }
        return getScheduleTime(year, monthOfYear + 1, dayOfMonth, hour, minute);//calendar中月份从0开始；
    }

    // 获得当天0点时间
    public static long getTodayMorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    // 获得当天24点时间
    public static long getTodayNight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTimeInMillis();
    }

    public static String YMD(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取年月
     *
     * @param day
     * @return
     */
    public static String getYearMonth(CalendarDay day) {
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy年MM月"
        );

        return format.format(day.getDate());
    }

    public static String getFormatDate(CalendarDay day) {
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy-MM-dd"
        );

        return format.format(day.getDate());
    }

    public static String getWeek() {
        Calendar cal = Calendar.getInstance();
        int i = cal.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
                return "";
        }
    }

    /**
     * 比较日期
     *
     * @param DATE1 选择日期
     * @param DATE2 被比较日期
     * @return
     */
    public static boolean compare_date(String DATE1, String DATE2) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        try {
            Date dt1 = simpleDateFormat.parse(DATE1);
            Date dt2 = simpleDateFormat.parse(DATE2);
            if (dt1.getTime() < dt2.getTime()) {
                //date 1小于 date 2
                return true;
            } else if (dt1.getTime() > dt2.getTime()) {
                //date 1大于 date 2
                return false;
            } else {
                //date 1等于 date 2
                return false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            return false;
        }

    }
}